﻿//https://leetcode.cn/problems/non-overlapping-intervals/

class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals)
    {
        //排序
        sort(intervals.begin(), intervals.end());
        int n = intervals.size();
        int ret = 0;
        int right = intervals[0][1];

        for (int i = 1; i < n; i++)
        {
            int a = intervals[i][0], b = intervals[i][1];
            if (a < right)
            {
                //重叠 删掉right大的区间
                ret++;
                right = min(b, right);
            }
            else
            {
                //不重叠 更新right
                right = b;
            }
        }

        return ret;
    }
};